package com.example.w.common.pojo;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.example.w.common.util.Utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * order
 * @author 
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order implements Serializable {
    /**
     * 订单id
     */
    private Long id;

    /**
     * 日期时间
     */
    private Date date;

    /**
     * 桌子id
     */
    private Integer tableId;

    /**
     * 总计
     */
    private BigDecimal total;

    /**
     * 支付状态，0表示未支付，1表示已支付
     */
    private Integer paystatus;


    private List<OrderItem> orderItemList;

    private static final long serialVersionUID = 1L;

    /**
     * 获取总金额
     */
    public BigDecimal getTotal() {
        total = new BigDecimal("0.00");
        for (OrderItem item: orderItemList) {
            total = total.add(item.getSubtotal());
        }
        return total;
    }

    /**
     * 获得菜品id
     */
    public long[] getMenuItemId() {
        long[] ids = new long[orderItemList.size()];
        System.out.println("Order.getMenuItemId: orderItemList=" + orderItemList + "\n" + orderItemList.size());
        for (int i = 0; i < ids.length; i++) {
            ids[i] = orderItemList.get(i).getMenuItemId();
        }
        return ids;
    }

    /**
     * 设置订单支付状态
     */
    public void setPaid(boolean paid) {
        this.paystatus = (paid? 1: 0);
    }

    public boolean isPaid() {
        return paystatus == 1;
    }

    /**
     * 添加订单项
     */
    public void addOrderItem(List<OrderItem> additionalOrderItems) {
        orderItemList.addAll(additionalOrderItems);
    }

    /**
     * 判断订单项基本信息是否完整
     */
    private boolean isOrderItemListInfoComplete() {
        boolean complete = !Utils.isEmpty(orderItemList);
        if (complete) {
            for (OrderItem orderItem: orderItemList){
                complete = complete && orderItem.isInfoCompleteForAddition();
            }
        }
        return complete;
    }

    /**
     * 检查添加订单必须信息
     */
    public boolean isInfoCompleteForAddition() {
        return isOrderItemListInfoComplete() && tableId != null;
    }

    /**
     * 检查加菜必须信息
     */
    public boolean isInfoCompleteForAdditionalMenuItem() {
        return isOrderItemListInfoComplete() && id != null;
    }

}